// 引入logo
const { logo_window } = require("../constant/index");

// 引入
const { getConfig } = require("../utils/getAndSetConfig");

const { app, BrowserWindow, Menu } = require("electron");

const path = require("path");

const RuneWindow = async () => {
  const windowStateKeeper = require("electron-window-state");
  let mainWindowState = windowStateKeeper({
    defaultWidth: 400,
    defaultHeight: 640,
    file: "runeWin.json",
    x: 1500,
    y: 200,
  });
  const win = new BrowserWindow({
    // width: 400,
    // height: 640,
    // x: 1500,
    // y: 200,
    x: mainWindowState.x,
    y: mainWindowState.y,
    width: mainWindowState.width,
    height: mainWindowState.height,
    // 窗口置顶
    alwaysOnTop: true,
    // 图标
    icon: logo_window,

    // 创建时是否显示窗口
    // show :false,

    webPreferences: {
      // 启用node
      // contextIsolation :true,
      // nodeIntegration: true,
      // contextIsolation: true,
      // 预加载
      preload: path.join(__dirname, "../preload/index.js"),
      nodeIntegration: true,
      webSecurity: false,
    },
  });

  if (app.isPackaged) {
    // 生产环境
    // null值取消顶部菜单栏
    // Menu.setApplicationMenu(null);

    win.loadFile(`file://${path.join("../dist/index.html")}`, {
      hash: "rune",
    });
  } else {
    // 测试环境
    win.loadURL("http://localhost:5000/#/rune");
    // win.loadFile(`file://${path.join("../dist/index.html")}`, {
    //   hash: "rune",
    // });
  }

  //  记忆窗口大小
  mainWindowState.manage(win);
  return win;
};

let win = null;
const createRuneWindow = async () => {
  const { heroOfChoice } = require("../axios/rune");

  setInterval(async () => {
    let data = await heroOfChoice();

    if (data.myTeam) {
      if (win == null) {
        // 打开符文页
        win = await RuneWindow();
      }
    } else {
      if (win != null) {
        win.destroy();
        win = null;
      }
    }
  }, 1000);
  return win;
};

module.exports = RuneWindow;
